#include "stdafx.h"
#include "timereminder.h"
#include "ui_timereminder.h"
#include "showpicture.h"

TimeReminder::TimeReminder(QWidget *parent)
	: QWidget(parent)
	, second(0)
{
	ui = new Ui::TimeReminder();
	ui->setupUi(this);
	ui->timeEdit->setTime(QTime(01, 00, 000));
	connect(ui->startButton, &QPushButton::clicked, this, &TimeReminder::startTimer);

	timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));

	startTimer();
}

TimeReminder::~TimeReminder()
{
	delete ui;
}

void TimeReminder::startTimer()
{
	auto text = ui->startButton->text();
	if (text == "start")
	{
		time = ui->timeEdit->time();
		second = time.hour() * 3600 + time.minute() * 60 + time.second();
		ui->startButton->setText("stop");
		timer->start(1000);
	}
	else
	{
		timer->stop();
		ui->startButton->setText("start");
	}
}

void TimeReminder::timerUpdate()
{
	if (second > 0)
	{
		--second;
		int hour = static_cast<int>(second / 3600);
		int minute = static_cast<int>((second - hour * 3600) / 60);
		int temp = second - hour * 3600 - minute * 60;
		ui->timeEdit->setTime(QTime(hour, minute, temp));
	}
	else
	{
		ShowPicture picture;
		picture.showFullScreen();
		picture.exec();

		second = time.hour() * 3600 + time.minute() * 60 + time.second();
		ui->timeEdit->setTime(QTime(0, 0, 0));
	}
}

void TimeReminder::closeEvent(QCloseEvent *event)
{
	hide();
	event->ignore();
}
